﻿/****************************************************************************
 * 2018.8 DESKTOP-4DTOKQJ
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Text;
using KangkangTodoList;

namespace QFramework.Example
{
	public class UIMainPanelData : UIPanelData
	{
		// TODO: Query Mgr's Data
	}

	public partial class UIMainPanel : UIPanel
	{
		protected override void InitUI(IUIData uiData = null)
		{
			mData = uiData as UIMainPanelData ?? new UIMainPanelData();
            //please add init code here
        
		}
        private void Start()
        {
            foreach (TodoItem todoItem in mTodoList.m_TodoList)
            {
                UIToDoItem ItemClone = Instantiate(TodoItem);
                ItemClone.gameObject.SetActive(true);
                ItemClone.transform.SetParent(TodoItemParent, false);
                ItemClone.UploadData(todoItem.Title, todoItem.IsCompletedProperty);
            }

            udd = new ExchangeTitleDelegate(HandlerExchangeTitleDelegate);
            usd = new ExchangeStringDelegate(HandlerString);
        }

        ExchangeTitleDelegate udd;
        ExchangeStringDelegate usd;
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.B))
            {
                udd(mTodoList.m_TodoList[0], mTodoList.m_TodoList[1]);
                UpdateView(0);
                UpdateView(1);
            }
            if (Input.GetKeyDown(KeyCode.A))
            {
                udd(mTodoList.m_TodoList[1], mTodoList.m_TodoList[2]);
                UpdateView(1);
                UpdateView(2);
            }
        }
        void HandlerExchangeTitleDelegate(TodoItem item1, TodoItem item2)
        {
            var temp = item1.Title;
            item1.Title = item2.Title;
            item2.Title = temp;
        }

        void HandlerString(string str1,string str2)
        {
            var temp = str1;
            str1 = str2;
            str2 = temp;
        }
        void UpdateView(int index)
        {
            TodoItemParent.GetChild(index).GetComponent<UIToDoItem>().UploadData(mTodoList.m_TodoList[index].Title, mTodoList.m_TodoList[index].IsCompletedProperty);
        }

        protected override void ProcessMsg (int eventId,QMsg msg)
		{
			throw new System.NotImplementedException ();
		}

		protected override void RegisterUIEvent()
		{
            AddTodoItemButton.onClick.AddListener(AddTodoListBtn_Func);
        }

        private void AddTodoListBtn_Func()
        {
            //GameObject go = new GameObject();
            //var todoItem = go.AddComponent<TodoItem>();
            //mTodoList.AddTodoItem(todoItem);
            //go.transform.SetParent(TodoItemParent);
            //todoItem.Title = "我是第" + mTodoList.m_TodoList.Count + "个TodoItem的标题";
            //todoItem.name = "TodoItem " + mTodoList.m_TodoList.Count;
            //Log.I("This is  No.{0} TodoItem Title", mTodoList.m_TodoList.Count);

            //------TodoList v0.0.6新增代码------//
            TodoItem mTodoItem = new TodoItem();
            mTodoList.AddTodoItem(mTodoItem);
            mTodoItem.Title = "新建的第" + mTodoList.m_TodoList.Count + "个TodoItem的标题";
            mTodoItem.IsCompletedProperty = false;

            UIToDoItem ItemClone = Instantiate(TodoItem);
            ItemClone.gameObject.SetActive(true);
            ItemClone.transform.SetParent(TodoItemParent, false);
            ItemClone.Finshed.isOn = mTodoItem.IsCompletedProperty;
            ItemClone.Title.text = mTodoItem.Title;
            Log.I("这是第{0}个TodoItem的Debug日志", mTodoList.m_TodoList.Count);
        }

        //string Encoding to UTF8
        public string GetUTF8String(string input)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            return utf8.GetString(utf8.GetBytes(input));
        }

        protected override void OnShow()
		{
			base.OnShow();
		}

		protected override void OnHide()
		{
			base.OnHide();
		}

		protected override void OnClose()
		{
			base.OnClose();
		}

		void ShowLog(string content)
		{
			Debug.Log("[ UIMainPanel:]" + content);
		}
	}
}